import { Injectable } from '@angular/core';
// import { AES, enc } from 'crypto-js';
// import { has, isNull, isUndefined, isNaN, each } from 'lodash';

@Injectable({
    providedIn: 'root'
})

export class SessionService {

    private json: Object = {};
    private tobeLocalSavedJson: Object = {};
    private tobeSessionSavedJson: Object = {};
    private timeoutId: any;

    /**
     * 得到session的值
     * @param {string} key
     * @returns {any}
     */
    public getSessionValue(key: string): any {
        try {
            let value: any = sessionStorage.getItem(key);
            value = JSON.parse(value);

            // console.log('get value', value);
            return value;
        } catch (e) {
            console.error(`localStorage access denied!`);
        }
    }

    /**
     * 添加session
     *
     * @param {string} key
     * @param value
     * @param {boolean} isPersistence
     */
    public putSessionValue(key: string, value: any): void {
        try {
            // console.log('put');

            value = JSON.stringify(value);
            sessionStorage.setItem(key, value)
        } catch (e) {
            console.error(`localStorage access denied!`);
        }
    }

    /**
     * 清除所有session
     */
    public clearSession() {
        console.log('clear');
        try {
            sessionStorage.clear();
        } catch (e) {
            console.error(`localStorage access denied!`);
        }
    }

    /**
     * 移除对应key的session
     * @param {string} key
     */
    public removeSessionValue(key: string) {
        try {
            sessionStorage.removeItem(key);
        } catch (e) {
            console.error(`localStorage access denied!`);
        }
    }
}